<p class=titulo align="center"><? echo $msg->get('XML_TITLE_GERA'); ?>
<p class=titulo align="center"><? echo $msg->get('XML_TITLE_MATRIZ'); ?></p>
<p class=titulo align="center"><? echo $msg->get('XML_GERA_MATRIZ_SUBTITLE_5'); ?></p>
<p>
<?
	include_once("config.php");
	require_once("verifica.php");
?>

<? if (strlen(@$arquivo)==0) { ?>

		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG01'); ?>
		<br>
		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_soma_xml.php" method="post">
		<input type="radio" name="arquivo" value="nao" unchecked><? echo $msg->get('XML_GERA_MSG02'); ?><br>
		<input type="radio" name="arquivo" value="sim" unchecked><? echo $msg->get('XML_GERA_MSG03'); ?><p>
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<? } else if ((@$arquivo=='sim')&((strlen(@$text_file)==0)||(!file_exists(@$text_file)))&(@$file_read!='ok')) { ?>

		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG04'); ?>
		<br>
		<form enctype=multipart/form-data action="index.php?inc=gera_prob_matriz_soma_xml.php" method="post">
		<a href=javascript:winopen('ajuda/ajdados1.php?idioma=PT_BR.php')><? echo $msg->get('XML_GERA_MSG05'); ?></a><p>
		<input type="file" name="text_file"><p>
		<? if ((strlen($text_file)!=0)&(!file_exists($text_file))) { ?>
              <? echo $msg->get('XML_GERA_MSG06'); ?>
        <? } ?>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<? } else if ( ((strlen(@$num_rows)==0)or($num_rows<1)or($num_cols<1)or(strlen($num_cols)==0)or($num_rows>10)or($num_cols>10))& ($arquivo=='nao'))  { ?>

		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_soma_xml.php" method="post" onSubmit="return tamanhomax(this)">
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG010'); ?>
		<b></b>
		<input type="text" size='2' maxlength='2' name="num_rows" value="<? echo @$num_rows ?>">x
		<input type="text" size='2' maxlength='2' name="num_cols" value="<? echo @$num_cols ?>">
		<br><br>
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<? } else if  (strlen(@$desc)==0 || !checaNumerico($A) || !checaNumerico($B)) { ?>


		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_soma_xml.php" method="post">
		
        <? if (($arquivo=='sim')&(@$file_read!='ok')) {
        $fp = fopen($text_file, "r");
		$vet_dados = file($text_file);
		$num_rows = trim($vet_dados[0]);
		$num_cols = trim($vet_dados[1]);
		$cont = 1;
		for ($i=0; $i<$num_rows; $i++){
			for ($j=0; $j<$num_cols; $j++){
			    $cont = $cont + 1;
			    $A[$i][$j]=trim($vet_dados[$cont]);
				} // end for j
		} // end for i
		for ($i=0; $i< $num_rows; $i++) {
		   for ($j=0; $j< $num_cols; $j++) {
			   $cont = $cont + 1;
			   $B[$i][$j]=trim($vet_dados[$cont]);
			   } // end for j
		} // end for i
		fclose($fp);
		$file_read = ok;
		}
		if (isset($A))
		{
			echo "<b><font color=\"#ff0000\">Os valores para as matrizes devem ser num&eacute;ricos</b>";
		}		
		?>

		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG010'); ?> <? echo @$num_rows ?> x <? echo @$num_cols ?>
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG02'); ?>
		<br><br>
		<? for ($i=0; $i<$num_rows; $i++) {	
			for ($j=0; $j<$num_cols; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG08'); ?> :
		<br><br>
		<? for ($i=0; $i<$num_rows; $i++) {	
			for ($j=0; $j<$num_cols; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="B[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$B[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG09'); ?>
		<b></b>
		<input type="text" size='50' maxlength='50' name="desc" value="<? echo @$desc ?>">
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG08'); ?><br>
		<TEXTAREA name="com" cols="50" rows="3"><? echo @$com ?></TEXTAREA>
		<p>
		<p align="left"><? echo $msg->get('XML_GERA_MSG09'); ?><br>
		<TEXTAREA name="ref" cols="50" rows="3"><? echo @$ref ?></TEXTAREA>
		<p>		
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
	    <input type="hidden" name="file_read" value="<? echo $file_read ?>">
      	<input type="hidden" name="num_rows" value="<? echo $num_rows ?>">
		<input type="hidden" name="num_cols" value="<? echo $num_cols ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_GERA_MSG010'); ?>>
		</form>

<? } else {
$id_demo=0;
//$usr_file = $DOCUMENT_ROOT."/user/".$id_usuario."/biblioteca.xml";
$usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
$dem_file = $DOCUMENT_ROOT."/user/".$id_demo."/biblioteca.xml";

		//Funcoes DOMXML Versao => 2.4.21
		function xmlentities($xml)
		{
			$entities = array ('<' => '&lt;','&' => '&amp;',);
			if (is_string($xml)) { $xml = strtr($xml, $entities);};
			return $xml;
		}
    function gera_bib($xml_file,$desc,$num_rows,$num_cols,$A,$B,$com,$ref) {
		if (file_exists($xml_file)) {
            $objeto = new DOMDocument();
            $objeto->preserveWhiteSpace=false;
            $objeto->load($xml_file);
		}
		else {
            $objeto = new DOMDocument('1.0');        
		}
        $objeto->formatOutput=true;
        if ($objeto->getelementsbytagname("problems")->length == 0)
        {
            $raiz=$objeto->createElement("problems");
            $raiz=$objeto->appendChild($raiz);
        }
        $raiz=$objeto->getelementsbytagname("problems")->item(0);
		$filho = $objeto->getelementsbytagname("matrices");
		if ($filho->length==0) {
            $filho = $objeto->createElement("matrices");
			$filho = $raiz->appendChild($filho);
		}
		$filho = $objeto->getelementsbytagname("matrices")->item(0);
        $matrix = $objeto->createElement("matrix");
		$matrix = $filho->appendChild($matrix);
        $description = $objeto->createElement("description",xmlentities(utf8_encode($desc)));
		$description = $matrix->appendChild($description);
        $rows = $objeto->createElement("num_rows",$num_rows);
		$rows = $matrix->appendChild($rows);
        $cols = $objeto->createElement("num_cols",$num_cols);
		$cols = $matrix->appendChild($cols);    
		for($i = 0; $i < $num_rows; $i++)
		{ 	$row = $objeto->createElement("row");
            $row = $matrix->appendChild($row);
			for($j = 0; $j < $num_cols; $j++)
			{ 	$x = $objeto->createElement("x",$A[$i][$j]);
                $x = $row->appendChild($x);
			}
		}
		for($i = 0; $i < $num_rows; $i++)
		{ 	$row = $objeto->createElement("row");
            $row = $matrix->appendchild($row);
			for($j = 0; $j < $num_cols; $j++)
			{ 	$x = $objeto->createElement("x",$B[$i][$j]);
                $x = $row->appendChild($x);
			}
		}
		if (strlen($com)) {
			$comen = $objeto->createElement("comments",xmlentities(utf8_encode($com)));    
            $comen = $matrix->appendChild($comen);
		}
		if (strlen($ref)) {
			$refer = $objeto->createElement("reference",xmlentities(utf8_encode($ref)));    
			$refer = $matrix->appendChild($refer);
		}
        $objeto->save($xml_file);
    }
gera_bib($usr_file,$desc,$num_rows,$num_cols,$A,$B,$com,$ref);
//gera_bib($dem_file,$desc,$num_rows,$num_cols,$A,$B,$com,$ref);

        ?>
		<p align="center"><? echo $msg->get('XML_GERA_MSG011'); ?></p>
        <?


} //end else

?>

		<form name="back" method="post" action="index.php?inc=gera_prob_matriz_soma_xml.php">
		<input class="button" type="submit" name="Submit" value="<? echo $msg->get('XML_BEGIN'); ?>">
		</form>